package com.myjunjie.blog.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.myjunjie.blog.dao.IArticleDao;
import com.myjunjie.blog.entity.Article;
import com.myjunjie.blog.util.StringHelper;
import com.myjunjie.blog.db.DBUtil;
import com.myjunjie.blog.db.IResultSetUtil;;

public class ArticleDaoImpl implements IArticleDao {

	/**
	 * 增加文章
	 * 
	 * @author 赖俊杰
	 * @param article
	 * @return ture or false
	 */
	public boolean addArticle(Article article) {
		String sql = "INSERT INTO article (title,mdtxt,category,publish_time) VALUES (?,?,?,?)";

		return DBUtil.executeUpdate(sql, article.getTitle(), article.getMdtxt(), article.getCategory(),
				article.getPublish_time()) > 0;
	}

	/**
	 * 删除文章
	 * 
	 * @author 赖俊杰
	 * @param article
	 * @return true or false
	 */
	public boolean deleArticleById(Article article) {
		String sql = "delete from article where id= ?";
		return DBUtil.executeUpdate(sql, article.getId()) > 0;
	}

	/**
	 * 更新文章
	 * 
	 * @author 赖俊杰
	 * @param article
	 * @return true or false
	 */
	public boolean upateArticle(Article article) {
		String sql = "UPDATE article SET title =?,mdtxt=?,category=?,publish_time=? where id = ?";

		return DBUtil.executeUpdate(sql, article.getTitle(), article.getMdtxt(), article.getCategory(),
				article.getPublish_time(), article.getId()) > 0;
	}

	/**
	 * 获取所有文章信息
	 *
	 * @author 赖俊杰
	 * @param startNum
	 * @param counted
	 */
	public JsonObject getList(String startNum, String counted) {
		// String sql = "SELECT id,title,mdtxt,category,publish_time FROM
		// article limit " + startNum + "," + counted;
		String sql = "SELECT id,title,category,publish_time FROM article ORDER BY  publish_time DESC limit " + startNum + "," + counted;

		System.out.println(sql);
		return (JsonObject) DBUtil.executeQuery(sql, new IResultSetUtil() {

			JsonArray articleArr = new JsonArray();

			public Object doHandler(ResultSet rs) throws SQLException {
				while (rs.next()) {
					JsonObject obj = new JsonObject();
					obj.addProperty("id", rs.getString("id"));
					obj.addProperty("title", rs.getString("title"));
					// obj.addProperty("mdtxt", rs.getString("mdtxt"));
					obj.addProperty("category", rs.getString("category"));
					obj.addProperty("publish_time", StringHelper.getDateByTimeStamp(rs.getLong("publish_time")));
					articleArr.add(obj);
				}
				JsonObject json = new JsonObject();
				json.add("articles", articleArr);
				return json;
			}
		});
	}

	public JsonObject getArticleById(String id) {
		String sql = "SELECT title,mdtxt,category,publish_time FROM article  where id = " + id;
		System.out.println(sql);
		return (JsonObject) DBUtil.executeQuery(sql, new IResultSetUtil() {
			JsonArray articleArr = new JsonArray();

			public Object doHandler(ResultSet rs) throws SQLException {
				while (rs.next()) {
					JsonObject obj = new JsonObject();
					obj.addProperty("title", rs.getString("title"));
					obj.addProperty("mdtxt", rs.getString("mdtxt"));
					obj.addProperty("category", rs.getString("category"));
					obj.addProperty("publish_time", StringHelper.getDateByTimeStamp(rs.getLong("publish_time")));
					articleArr.add(obj);
				}
				JsonObject json = new JsonObject();
				json.add("articles", articleArr);
				return json;
			}
		});
	}

	/**
	 * 根据分类查询
	 * 
	 * @author 赖俊杰
	 * @param category
	 * @param startNum
	 * @param counted
	 * @return 返回Article
	 */
	public JsonObject getListByCategory(String category, String startNum, String counted) {
		String sql = "SELECT id,title,mdtxt,publish_time FROM article where category = ? ORDER BY  publish_time DESC limit " + startNum + " , "
				+ counted ;
		return (JsonObject) DBUtil.executeQuery(sql, new IResultSetUtil() {
			JsonArray articleArr = new JsonArray();

			public Object doHandler(ResultSet rs) throws SQLException {
				while (rs.next()) {
					JsonObject article = new JsonObject();

					article.addProperty("id", rs.getString("id"));
					article.addProperty("title", rs.getString("title"));
					article.addProperty("publish_time", StringHelper.getDateByTimeStamp(rs.getLong("publish_time")));

					articleArr.add(article);
				}
				JsonObject json = new JsonObject();
				json.add("articles", articleArr);
				return json;
			}
		}, category);
	}

	/**
	 * 查询所有分类
	 * 
	 * @author 赖俊杰
	 * @param
	 * 
	 */
	public JsonObject getCategory() {
		String sql = "SELECT DISTINCT category FROM article";

		return (JsonObject) DBUtil.executeQuery(sql, new IResultSetUtil() {
			JsonArray articleArr = new JsonArray();

			public Object doHandler(ResultSet rs) throws SQLException {
				while (rs.next()) {
					JsonObject obj = new JsonObject();
					obj.addProperty("category", rs.getString("category"));
					articleArr.add(obj);
				}
				JsonObject json = new JsonObject();
				json.add("articles", articleArr);
				return json;
			}
		});
	}

}
